﻿using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.Dropinfo;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Minecraft.ServerHall.Cmd
{
	public class MonsterKill : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Monster_MonsterKill;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<MonsterKillReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.monsterId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：monsterId 为空）" });
				return;
			}

			Mod_Actor mod_Actor = CsvConfig.GetMod_ActorInfoById(req.monsterId);
			if (mod_Actor == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mod_Actor 配置表信息不存在）" });
				return;
			}
			Mod_Drop mod_Drop = CsvConfig.GetMod_DropInfoById(mod_Actor.Items_drop);
			if (mod_Drop == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mod_Drop 配置表信息不存在）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;
			if (map.isMoving_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：正处于大地图界面的移动状态，操作非法）" });
				return;
			}

			var timeNow = DateTimeOffset.Now;

			List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList = new List<DropinfoDropResourceInfo>();
			List<KeyValueEnumAttributeInt> dropAttributeList = new List<KeyValueEnumAttributeInt>();
			List<KeyValueEnumAttributeInt> attributeList = new List<KeyValueEnumAttributeInt>();
			List<BackpackCellInfo> cellList = new List<BackpackCellInfo>();

			int dropId = 0;
			if (map.SourceMapId == MinecraftConfig.BaseMapId) //基地中的怪目前只来自于尸潮
			{
				if (!session.minecraftSessionInfo.player.IsCanBrushMonster_Convert)
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：目前还未达到基地尸潮刷怪的条件）" });
					return;
				}
				var zombiesbrushInfo = session.minecraftSessionInfo.zombiesbrushInfo;
				var zombiesInfoList = zombiesbrushInfo.GetZombiesItemIdAndNumsList_Convert();
				var zombiesInfo = zombiesInfoList.FirstOrDefault(m => m.ActorId == mod_Actor.Id);
				if (zombiesInfo == null)
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：该波尸潮不包含此类型的僵尸【session.minecraftSessionInfo.zombiesbrushInfo：{session.minecraftSessionInfo.zombiesbrushInfo.ZombieAttackId}，mod_Actor.Id：{mod_Actor.Id}】）" });
					return;
				}
				if (zombiesInfo.Num <= 0)
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：该类型的僵尸数量为0【session.minecraftSessionInfo.zombiesbrushInfo：{session.minecraftSessionInfo.zombiesbrushInfo.ZombieAttackId}，mod_Actor.Id：{mod_Actor.Id}】）" });
					return;
				}
				//比较刷怪的时间区间
				if (timeNow >= zombiesbrushInfo.ZombieAttackStartTime && timeNow < zombiesbrushInfo.ZombieAttackFinishTime)
				{
					zombiesInfo.Num--;
					zombiesbrushInfo.TransformListToOutput_UpgradeMaterialGoodsItemIdsPosAndNums_Convert(zombiesInfo);
					zombiesInfoList = zombiesbrushInfo.GetZombiesItemIdAndNumsList_Convert();
					if (zombiesInfoList.All(m => m.Num <= 0))
					{
						//怪物全部杀光，胜利
						zombiesbrushInfo.ZombieAttackFinishTime = timeNow;
						//重新生成新的尸潮信息数据
						session.SetNewZombiesbrushInfo();
					}
					else
					{
						//继续刷怪，不做处理
					}
				}
				else
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：尸潮刷怪的时间区间错误，在杀怪前，请先调用尸潮信息查询接口，先在基地生成怪物）" });
					return;
				}
			}
			else
			{
				//获取掉落的物品
				dropinfoDropResourceInfoList = session.GetDropinfoDropRandomResourceInfoList(mod_Drop.Id);
				var baseResp = session.DropAddExp(dropinfoDropResourceInfoList, dropAttributeList, attributeList);
				if (baseResp.RespLevel != RespLevelEnum.Success)
				{
					session.Send(defCommand, req, baseResp);
					return;
				}

				//将掉落的物品保存起来
				var dropinfo = new DropinfoModel
				{
					DropGoodsFrom = (int)EnumGoodsBelongsToType.MonsterCorpseAndDismantleFurniture,
					DropId = session.GetNewDropInfoId(),
					DropTime = timeNow,
					MapId = map.SourceMapId,
					PlayerId = session.minecraftSessionInfo.player.PlayerId,
				};
				session.minecraftSessionInfo.dropinfoList.Add(dropinfo);
				var goodsList = new List<GoodsModel>();
				int position = 0;
				foreach (var dropinfoDropResourceInfo in dropinfoDropResourceInfoList)
				{
					var goodsModel = new GoodsModel
					{
						BelongsTo = (int)EnumGoodsBelongsToType.MonsterCorpseAndDismantleFurniture,
						GoodsId = StringHelper.GetGuidStr(),
						GoodsItemId = dropinfoDropResourceInfo.GoodsItemId,
						GoodsPosition = position,
						guid = dropinfo.DropId.ToString(),
						PlayerId = session.minecraftSessionInfo.player.PlayerId,
						Num = dropinfoDropResourceInfo.Num,
						WastageValue = 100
					};
					goodsList.Add(goodsModel);
					position++;
				}
				session.minecraftSessionInfo.goodsList.AddRange(goodsList);

				//给客户端物品信息
				cellList = goodsList.GetBackpackGoodsCellInfo();
				dropId = dropinfo.DropId;
			}

			var resp = new MonsterKillResp
			{
				entityId = req.entityId,
				dropId = dropId,
				dropinfoDropResourceInfoList = dropinfoDropResourceInfoList,
				CellList = cellList,
				dropAttributeList = dropAttributeList,
				attributeList = attributeList
			};
			session.Send(defCommand, req, resp);
		}
	}
}
